﻿using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;

using Gonte.Wpf.Controls.ResourceManagers;
using Gonte.Wpf.Controls.ViewModel;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyWpfControlsTest
{
    [TestClass()]
    public class ContentViewModelTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void ContentViewModelConstructorTest()
        {
            // Set up the images
            BitmapImage collapsedImage = new BitmapImage();
            string collapsedImageUri = @"pack://application:,,/Images/CollapsedRoot.jpg";
            ImageManager_Accessor.Images[collapsedImageUri] = collapsedImage;

            BitmapImage expandedImage = new BitmapImage();
            string expandedImageUri = @"pack://application:,,/Images/ExpandedRoot.jpg";
            ImageManager_Accessor.Images[expandedImageUri] = expandedImage;

            // Set up the view model info
            ContentViewModelInfo viewModelInfo = (ContentViewModelInfo)ContentViewModelInfo.GetInfo("TestViewModel");
            viewModelInfo.CollapsedIconUri = collapsedImageUri;
            viewModelInfo.ExpandedIconUri = expandedImageUri;

            ContentViewModel viewModel = new ContentViewModel(viewModelInfo, null, new object());
            const int count = 5;
            viewModel.AddChildren = delegate(ContentViewModel vm)
            {
                ItemViewModelInfo itemViewModelInfo = ItemViewModelInfo.GetInfo("ContentViewModelConstructorTestItemViewModelInfo");

                for (int i = 0; i < count; ++i)
                {
                    ItemViewModel itemViewModel = new ItemViewModel(itemViewModelInfo, vm, new object());
                    vm.Children.Add(itemViewModel);
                }
            };

            viewModel.LoadChildren();
            Assert.AreEqual(count, viewModel.Children.Count);

            Assert.AreEqual(false, viewModel.IsExpanded); // The tree is collapsed by default
            Assert.AreEqual(collapsedImage, viewModel.IconSource);

            viewModel.IsExpanded = true;
            Assert.AreEqual(expandedImage, viewModel.IconSource);


        }
    }
}
